package com.upcode.weighing.oauthserver.config;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 配置ETC发行数据源及Mapper文件
 */
@Configuration
@MapperScan(basePackages = {"com.upcode.weighing.mapper"},sqlSessionFactoryRef = "sqlSessionFactory")
public class EtcIssueMyBatisDbConfig {

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    @Autowired
    private MybatisPlusProperties properties;

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception{
    	
    	
    	MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
        mybatisPlus.setDataSource(dataSource);
        mybatisPlus.setVfs(SpringBootVFS.class);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resource=resolver.getResources("classpath:mapper/*.xml");
        mybatisPlus.setMapperLocations(resource);
        MybatisConfiguration mc = new MybatisConfiguration();
        mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        mc.addInterceptor(paginationInterceptor2());
        mybatisPlus.setConfiguration(mc);
        return mybatisPlus.getObject();
    }
    @Bean
    public SqlSessionTemplate sqlSession() throws Exception{
        SqlSessionTemplate sqlSessionTemplate=new SqlSessionTemplate(sqlSessionFactory());

        return sqlSessionTemplate;
    }

    @Bean
    public PaginationInterceptor paginationInterceptor2() {
        return new PaginationInterceptor();
    }
}